<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                :disabled="showButton('val')"
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click.native="addRoleFn"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList.rows">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="obj">
                  <el-button size="small" type="success" @click="assignPermFn(obj.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRoleFn(obj.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoleFn(obj.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :page-size="pageInfo.pagesize"
                :total="roleList.total"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" autosize resize="none" :rows="3" disabled style="width:400px;" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>

        <el-dialog title="编辑角色" :visible.sync="showdialog" @close="comfirmFn">
          <el-form ref="roleForm" :model="formData" :rules="rules">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="formData.description" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancelFn">取 消</el-button>
            <el-button type="primary" @click="comfirmFn">确 定</el-button>
          </div>
        </el-dialog>
        <el-dialog title="权限设置" :visible="isShowPerm" @close="cancelPermFn">
          <el-tree
            ref="permTree"
            :data="permList"
            :props="{label:'name'}"
            default-expand-all
            node-key="id"
            show-checkbox
            check-strictly
          />
          <template #footer>
            <el-row type="flex" justify="center">
              <el-button size="small" type="primary" @click="comfrimPermFn">确定</el-button>
              <el-button size="small" @click="cancelPermFn">取消</el-button>
            </el-row>
          </template>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, delRole, getRoleDetails, addRole, editRole, assignRolePerm } from '@/api/setting'
import { getPermission } from '@/api/permission'
import { updateDepartmentList } from '@/utils/index'
export default {
  data() {
    return {
      roleList: {},
      companyInfo: {},
      pageInfo: {
        page: 1,
        pagesize: 10
      },
      showdialog: false,
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 12, min: 1, message: '角色名字要求 1-12 个字符内' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 50, min: 1, message: '角色描述要求 1-50 个字符内' }
        ]
      },
      isShowPerm: false,
      permList: [],
      roleId: ''
    }
  },
  created() {
    this.companyInfoFn()
    this.roleFn()
    this.getPermissionFn()
  },
  methods: {
    // showButton(val) {
    //   if (!this.$store.state.user.userInfo.roles) {
    //     return false
    //   }
    //   return this.$store.state.user.userInfo.roles.points.includes(val)
    // },
    // 获取公司信息函数
    async companyInfoFn() {
      this.companyInfo = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    // 获取角色信息函数
    async roleFn() {
      const res = await getRoleList(this.pageInfo)
      this.roleList = res
    },
    // 分页换页
    currentChange(newPage) {
      this.pageInfo.page = newPage
      this.roleFn()
    },
    // 新增角色
    addRoleFn() {
      this.showdialog = true
    },
    // 删除角色
    async delRoleFn(id) {
      await this.$confirm('是否确定删除')
      await delRole(id)
      this.$message.success('删除成功')
      this.roleFn()
    },
    // 编辑角色内容
    async editRoleFn(id) {
      this.formData = await getRoleDetails(id)
      this.showdialog = true
    },
    // 获取所有权限
    async getPermissionFn() {
      this.permList = await updateDepartmentList(await getPermission(), '0')
    },
    // 分配权限
    async assignPermFn(id) {
      this.roleId = id
      const { permIds } = await getRoleDetails(id)
      this.isShowPerm = true
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    // 权限分配提交
    comfrimPermFn() {
      assignRolePerm({
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys()
      })
      this.$message.success('修改成功')
      this.isShowPerm = false
    },
    // 关闭分配权限弹窗
    cancelPermFn() {
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys([])
      })
      this.isShowPerm = false
    },
    // 取消
    cancelFn() {
      this.showdialog = false
      this.formData = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    // 确定
    async comfirmFn() {
      await this.$refs.roleForm.validate()
      if (this.formData.id) {
        await editRole(this.formData)
      } else {
        await addRole(this.formData)
      }
      this.showdialog = false
      this.formData = {
        name: '',
        describe: ''
      }
      this.$refs.roleForm.resetFields()
      this.roleFn()
    }
  }
}
</script>

<style>

</style>
